# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=dpkg
pkgver=1.21.9
pkgrel=0
pkgdesc="The Debian Package Manager"
url="https://wiki.debian.org/Teams/Dpkg"
arch="all"
license="GPL-2.0-or-later BSD-2-Clause"
depends="tar xz"
checkdepends="gzip coreutils"
makedepends="bzip2-dev linux-headers perl zlib-dev po4a"
subpackages="$pkgname-doc $pkgname-dev $pkgname-zsh-completion:zshcomp:noarch"
source="https://deb.debian.org/debian/pool/main/d/dpkg/dpkg_$pkgver.tar.xz"

# secfixes:
#   1.21.8-r0:
#     - CVE-2022-1664

prepare() {
	default_prepare

	# On the builders /home/buildozer has S_ISGID set this is
	# inherited in $srcdir and causes some dpkg test to end up with
	# a permission mismatch. To fix this remove the S_ISGID from
	# $srcdir.
	chmod -R g-s "$srcdir"

	# This test fails with:
	#
	#   dpkg-checkbuilddeps: error: Unmet build dependencies: build-essential:native
	#
	# Probably unfixable as we don't have a build-essential dpkg package.
	sed -i "$builddir"/scripts/Makefile.in \
		-e '/dpkg_buildpackage\.t/d'
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--with-libz \
		--with-libbz2 \
		--disable-dselect \
		--disable-start-stop-daemon \
		--disable-nls \
		--disable-static \
		--with-zshcompletionsdir=/usr/share/zsh/site-functions
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	# Add missing dir & files
	install -d "$pkgdir/var/$pkgname"/updates/

	touch "$pkgdir/var/lib/$pkgname"/status
	touch "$pkgdir/var/lib/$pkgname"/available
	rm "$pkgdir"/usr/lib/libdpkg.a
}

dev() {
	default_dev
	pkgdesc="Debian package development tools"
	depends="perl"
	mkdir -p "$subpkgdir"/usr/bin "$subpkgdir"/usr/share \
		"$subpkgdir"/usr/lib/dpkg

	mv "$pkgdir"/usr/bin/dpkg-architecture \
		"$pkgdir"/usr/bin/dpkg-buildpackage \
		"$pkgdir"/usr/bin/dpkg-checkbuilddeps \
		"$pkgdir"/usr/bin/dpkg-distaddfile \
		"$pkgdir"/usr/bin/dpkg-genchanges \
		"$pkgdir"/usr/bin/dpkg-gencontrol \
		"$pkgdir"/usr/bin/dpkg-gensymbols \
		"$pkgdir"/usr/bin/dpkg-mergechangelogs \
		"$pkgdir"/usr/bin/dpkg-name \
		"$pkgdir"/usr/bin/dpkg-parsechangelog \
		"$pkgdir"/usr/bin/dpkg-scanpackages \
		"$pkgdir"/usr/bin/dpkg-scansources \
		"$pkgdir"/usr/bin/dpkg-shlibdeps \
		"$pkgdir"/usr/bin/dpkg-source \
		"$pkgdir"/usr/bin/dpkg-vendor \
		"$subpkgdir"/usr/bin/

	mv "$pkgdir"/usr/share/perl* "$subpkgdir"/usr/share/
}

sha512sums="
0f0b9b936a025f9d905dc35c030293d9a1bdcbed640187691ae2319d99d04dac954102b1270301b12b633c48f08d6c6af0f6f21df4afb2e34a0f0a37127fd238  dpkg_1.21.9.tar.xz
"
